#include "CDataParser.h"

int CDataParser::LoadAnimation(string _szFileName, CAnimatedObject* _pOwner)
{
	string szFilePath = "././Assets/Animation Data/";
	string temp = _szFileName;
	szFilePath += _szFileName;

	TiXmlDocument doc;

	int _nReturn = -1;

	if (doc.LoadFile(szFilePath.c_str()) == false)
		return _nReturn;

	TiXmlElement* pRoot = doc.RootElement();
	if (pRoot == NULL)
		return _nReturn;

	TiXmlElement* pAnimation = pRoot->FirstChildElement("Animation");
	while (pAnimation != NULL)
	{
		CAnimation *tmpAnim = new CAnimation(_pOwner);

		// Set whether or not Animation is looping
		int IsLooping;
		pAnimation->Attribute("IsLooping", &IsLooping);

		if (IsLooping)
			tmpAnim->SetIsLooping(true);
		else
			tmpAnim->SetIsLooping(false);

		TiXmlElement* pImageName = pAnimation->FirstChildElement("FileName");

		const char *szImageName = pImageName->GetText();

		if (szImageName)
		{
			char buffer[128] = { 0 };
			strcpy_s(buffer, _countof(buffer), szImageName);

			szFilePath = "./Resource/graphics/Animations/";
			string szFileName = buffer;
			szFilePath += szFileName;

			int nR, nG, nB;
			pImageName->Attribute("bcR", &nR);
			pImageName->Attribute("bcG", &nG);
			pImageName->Attribute("bcB", &nB);
			tmpAnim->SetImageID(CAssetManager_AL::GetInstance()->CreateTexture(szFilePath.c_str()));
		}

		TiXmlElement* pFrame = pAnimation->FirstChildElement("Frame");
		while (pFrame != NULL)
		{
			SFrame *tmpFrame = new SFrame();

			// Display Rect
			int nX, nY, nWidth, nHeight;
			pFrame->Attribute("dX", &nX);
			pFrame->Attribute("dY", &nY);
			pFrame->Attribute("dW", &nWidth);
			pFrame->Attribute("dH", &nHeight);

			tmpFrame->DisplayRect.left = nX;
			tmpFrame->DisplayRect.top = nY;
			tmpFrame->DisplayRect.right = tmpFrame->DisplayRect.left + nWidth;
			tmpFrame->DisplayRect.bottom = tmpFrame->DisplayRect.top + nHeight;

			// Collision Rect
			pFrame->Attribute("cX", &nX);
			pFrame->Attribute("cY", &nY);
			pFrame->Attribute("cW", &nWidth);
			pFrame->Attribute("cH", &nHeight);

			tmpFrame->CollisionRect.left = nX;
			tmpFrame->CollisionRect.right = nY;
			tmpFrame->CollisionRect.right = tmpFrame->CollisionRect.left + nWidth;
			tmpFrame->CollisionRect.bottom = tmpFrame->CollisionRect.top + nHeight;

			// Anchor
			pFrame->Attribute("aX", &nX);
			pFrame->Attribute("aY", &nY);

			tmpFrame->Anchor.x = nX;
			tmpFrame->Anchor.y = nY;

			//Time
			pFrame->Attribute("Time", &tmpFrame->dFrameLength);

			// Event
			const char *szEvent = pImageName->GetText();

			if (szEvent)
			{
				char buffer[128];
				strcpy_s(buffer, _countof(buffer), szEvent);
				tmpFrame->Event = buffer;
			}
			tmpAnim->AddFrame(tmpFrame);
			pFrame = pFrame->NextSiblingElement("Frame");
		}
		_pOwner->GetAnimationManager()->AddAnimation(tmpAnim);
		if (_nReturn < 0)
			_nReturn = _pOwner->GetAnimationManager()->GetAnimVectorSize() - 1;
		pAnimation = pAnimation->NextSiblingElement("Animation");
	}

	return _nReturn;
}
